package cn.dongqihong.produceconsumption;

import java.util.ArrayList;
import java.util.List;

/**
 * 篮子
 * Created by DongQihong on 2016/12/15.
 */
public class Basket {
    /**
     * 容量
     */
    private int capacity = 10;
    /**
     * 当前数量
     */
    private int size = 0;
    private List<Bun> buns = new ArrayList<>(capacity);

    /**
     * 添加包子
     *
     * @param bun
     */
    public synchronized void in(Bun bun) {
        this.notify();
        while (size >= capacity) { //如果篮子满了
            System.out.println("篮子满了");
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        size++;
        buns.add(bun);
        System.out.println("放入包子，现数量为：" + size);
    }

    /**
     * 出包子
     */
    public synchronized Bun out() {
        this.notify();
        while (size <= 0) { //如果篮子空了
            System.out.println("篮子空了");
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Bun bun = buns.remove(0);
        size--;
        System.out.println("取出包子，现数量为：" + size);
        return bun;
    }
}
